import struct,binascii,math
str = struct.pack('>f', 3.14)
str1 = binascii.hexlify(str)
print(str[0])
print('str=',type(str1))
print(str1.decode())
print(type(str1.decode()))
print('{:02X}{:02X}{:02X}{:02X}'.format(str[0],str[1],str[2],str[3]))

#coding:utf-8
a = "hello world"

#先把a转换成二进制数据然后在用十六进制表示
c = binascii.hexlify(a)#和a2b_hex()功能是一样的,但是推荐用这个函数,具体不清楚= =
print('{0:10s}'.format(c))
for i in range(0,len(b),2):
    print(b[i:i+2])
#到这是不是发现了,利用b2a_hex()返回的字符串长度为原串的两倍,因为转换为十六进制,一个字节用两个字节表示了
print ('{0:10s}'.format('hexlify'))


#与b2a_hex相反
print(binascii.a2b_hex(b))
print (binascii.unhexlify(c))
'''
result:
b2a_hex    68 65 6c 6c 6f 20 77 6f 72 6c 64
hexlify    68 65 6c 6c 6f 20 77 6f 72 6c 64
ord        68 65 6c 6c 6f 20 77 6f 72 6c 64
hello world
hello world
'''
